<!DOCTYPE html>












  


<html class="theme-next gemini use-motion" lang="zh-CN">
<head><meta name="generator" content="Hexo 3.8.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">


















  
  
    
  
  <link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/fancybox/3.5.6/jquery.fancybox.min.css">







<link rel="stylesheet" href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2">

<link rel="stylesheet" href="/css/main.css?v=7.1.1">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=7.1.1">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32.ico?v=7.1.1">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16.ico?v=7.1.1">


  <link rel="mask-icon" href="/images/logo.svg?v=7.1.1" color="#222">







<script id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Gemini',
    version: '7.1.1',
    sidebar: {"position":"left","display":"post","offset":12,"onmobile":false,"dimmer":false},
    back2top: true,
    back2top_sidebar: true,
    fancybox: true,
    fastclick: false,
    lazyload: false,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>


  




  <meta name="description" content="DevOpS 环节中，代码托管是必不可少的。本文介绍如何利用 docker-compose 搭建 Gitlab 服务。">
<meta name="keywords" content="DevOps CICD">
<meta property="og:type" content="article">
<meta property="og:title" content="使用 Dockcer-Compose 安装 Gitlab 服务">
<meta property="og:url" content="https://michael728.github.io/2019/06/15/docker-compose-install-gitlab-runner/index.html">
<meta property="og:site_name" content="Michael翔">
<meta property="og:description" content="DevOpS 环节中，代码托管是必不可少的。本文介绍如何利用 docker-compose 搭建 Gitlab 服务。">
<meta property="og:locale" content="zh-CN">
<meta property="og:image" content="https://ws1.sinaimg.cn/mw690/6d9475f6ly1g41w3v73rtj20m80ci3yj.jpg">
<meta property="og:image" content="https://ws1.sinaimg.cn/mw690/6d9475f6ly1g41uqkxowbj20ze0b775y.jpg">
<meta property="og:image" content="https://ws1.sinaimg.cn/mw690/6d9475f6ly1g41w79zw81j20p10ddq4x.jpg">
<meta property="og:image" content="https://ws1.sinaimg.cn/mw690/6d9475f6ly1g41yzy7i10j20z50e741d.jpg">
<meta property="og:image" content="https://ws1.sinaimg.cn/mw690/6d9475f6ly1g4221cdnkqj20yw0iaacy.jpg">
<meta property="og:image" content="https://ws1.sinaimg.cn/mw690/6d9475f6ly1g421vw7in7j21jp0ekwgk.jpg">
<meta property="og:updated_time" content="2020-01-05T15:47:56.116Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="使用 Dockcer-Compose 安装 Gitlab 服务">
<meta name="twitter:description" content="DevOpS 环节中，代码托管是必不可少的。本文介绍如何利用 docker-compose 搭建 Gitlab 服务。">
<meta name="twitter:image" content="https://ws1.sinaimg.cn/mw690/6d9475f6ly1g41w3v73rtj20m80ci3yj.jpg">



  <link rel="alternate" href="/atom.xml" title="Michael翔" type="application/atom+xml">



  
  
  <link rel="canonical" href="https://michael728.github.io/2019/06/15/docker-compose-install-gitlab-runner/">



<script id="page.configurations">
  CONFIG.page = {
    sidebar: "",
  };
</script>

  <title>使用 Dockcer-Compose 安装 Gitlab 服务 | Michael翔</title>
  






  <script>
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?1c9958c951d5a7661571422e0340e156";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>







  <noscript>
  <style>
  .use-motion .motion-element,
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-title { opacity: initial; }

  .use-motion .logo,
  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

  <meta name="referrer" content="never">
</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-CN">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Michael翔</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
    
      
        <h1 class="site-subtitle" itemprop="description">因上努力，果上随缘！</h1>
      
    
    
  </div>

  <div class="site-nav-toggle">
    <button aria-label="切换导航栏">
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>



<nav class="site-nav">
  
    <ul id="menu" class="menu">
      
        
        
        
          
          <li class="menu-item menu-item-home">

    
    
    
      
    

    

    <a href="/" rel="section"><i class="menu-item-icon fa fa-fw fa-home"></i> <br>首页</a>

  </li>
        
        
        
          
          <li class="menu-item menu-item-tags">

    
    
    
      
    

    

    <a href="/tags/" rel="section"><i class="menu-item-icon fa fa-fw fa-tags"></i> <br>标签</a>

  </li>
        
        
        
          
          <li class="menu-item menu-item-categories">

    
    
    
      
    

    

    <a href="/categories/" rel="section"><i class="menu-item-icon fa fa-fw fa-th"></i> <br>分类</a>

  </li>
        
        
        
          
          <li class="menu-item menu-item-archives">

    
    
    
      
    

    

    <a href="/archives/" rel="section"><i class="menu-item-icon fa fa-fw fa-archive"></i> <br>归档</a>

  </li>
        
        
        
          
          <li class="menu-item menu-item-tools">

    
    
    
      
    

    

    <a href="/tools/" rel="section"><i class="menu-item-icon fa fa-fw fa-location-arrow"></i> <br>利器</a>

  </li>
        
        
        
          
          <li class="menu-item menu-item-books">

    
    
    
      
    

    

    <a href="/books" rel="section"><i class="menu-item-icon fa fa-fw fa-book"></i> <br>阅读</a>

  </li>
        
        
        
          
          <li class="menu-item menu-item-movies">

    
    
    
      
    

    

    <a href="/movies" rel="section"><i class="menu-item-icon fa fa-fw fa-film"></i> <br>观影</a>

  </li>
        
        
        
          
          <li class="menu-item menu-item-think">

    
    
    
      
    

    

    <a href="/think" rel="section"><i class="menu-item-icon fa fa-fw fa-lightbulb-o"></i> <br>一言</a>

  </li>
        
        
        
          
          <li class="menu-item menu-item-about">

    
    
    
      
    

    

    <a href="/about/" rel="section"><i class="menu-item-icon fa fa-fw fa-user"></i> <br>关于</a>

  </li>

      
      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" class="popup-trigger">
          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br>搜索</a>
        </li>
      
    </ul>
  

  

  
    <div class="site-search">
      
  <div class="popup search-popup local-search-popup">
  <div class="local-search-header clearfix">
    <span class="search-icon">
      <i class="fa fa-search"></i>
    </span>
    <span class="popup-btn-close">
      <i class="fa fa-times-circle"></i>
    </span>
    <div class="local-search-input-wrapper">
      <input autocomplete="off" placeholder="搜索..." spellcheck="false" type="text" id="local-search-input">
    </div>
  </div>
  <div id="local-search-result"></div>
</div>



    </div>
  
</nav>



  



</div>
    </header>

    
  
  

  

  <a href="https://github.com/michael728" class="github-corner" title="Follow me on GitHub" aria-label="Follow me on GitHub" rel="noopener" target="_blank"><svg width="80" height="80" viewbox="0 0 250 250" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"/><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"/><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"/></svg></a>



    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          
            

          
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://michael728.github.io/2019/06/15/docker-compose-install-gitlab-runner/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Michael翔">
      <meta itemprop="description" content="DevOps CICD Blog 持续学习">
      <meta itemprop="image" content="/images/logo.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Michael翔">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">使用 Dockcer-Compose 安装 Gitlab 服务

              
            
          </h2>
        

        <div class="post-meta">
          <span class="post-time">

            
            
            

            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              

              
                
              

              <time title="创建时间：2019-06-15 13:38:59" itemprop="dateCreated datePublished" datetime="2019-06-15T13:38:59+00:00">2019-06-15</time>
            

            
              

              
                
                <span class="post-meta-divider">|</span>
                

                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                
                  <span class="post-meta-item-text">更新于</span>
                
                <time title="修改时间：2020-01-05 15:47:56" itemprop="dateModified" datetime="2020-01-05T15:47:56+00:00">2020-01-05</time>
              
            
          </span>

          

          
            
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p><img src="https://ws1.sinaimg.cn/mw690/6d9475f6ly1g41w3v73rtj20m80ci3yj.jpg" alt></p>
<p>DevOpS 环节中，代码托管是必不可少的。本文介绍如何利用 docker-compose 搭建 Gitlab 服务。</p>
<a id="more"></a>
<h2 id="gitlab-镜像"><a href="#gitlab-镜像" class="headerlink" title="gitlab 镜像"></a>gitlab 镜像</h2><p>gitlab 分为两个版本：</p>
<ul>
<li><code>gitlab-ce</code> 社区版</li>
<li><code>gitlab-ee</code> 企业收费版</li>
</ul>
<p>本文的镜像，使用的是社区版本。社区办的镜像，可以在<a href="https://hub.docker.com/r/gitlab/gitlab-ce" target="_blank" rel="noopener">gitlab/gitlab-ce
</a> 查看。</p>
<h2 id="硬件要求"><a href="#硬件要求" class="headerlink" title="硬件要求"></a>硬件要求</h2><p>查看 <a href="https://docs.gitlab.com/ce/install/requirements.html#memory" target="_blank" rel="noopener">官宣-Requirements</a></p>
<h3 id="CPU"><a href="#CPU" class="headerlink" title="CPU"></a>CPU</h3><ul>
<li>1 core supports up to 100 users but the application can be a bit slower due to having all workers and background jobs running on the same core</li>
<li>2 cores is the recommended number of cores and supports up to 500 users</li>
<li>4 cores supports up to 2,000 users</li>
<li>8 cores supports up to 5,000 users</li>
<li>16 cores supports up to 10,000 users</li>
<li>32 cores supports up to 20,000 users</li>
<li>64 cores supports up to 40,000 users</li>
<li>More users? Run it on <a href="https://about.gitlab.com/solutions/high-availability/" target="_blank" rel="noopener">multiple application servers</a></li>
</ul>
<h3 id="Memory"><a href="#Memory" class="headerlink" title="Memory"></a>Memory</h3><ul>
<li>4GB RAM + 4GB swap supports up to 100 users but it will be very slow</li>
<li>8GB RAM is the recommended memory size for all installations and supports up to 100 users</li>
<li>16GB RAM supports up to 2,000 users</li>
<li>32GB RAM supports up to 4,000 users</li>
<li>64GB RAM supports up to 8,000 users</li>
<li>128GB RAM supports up to 16,000 users</li>
<li>256GB RAM supports up to 32,000 users</li>
<li>More users? Run it on multiple application servers</li>
</ul>
<h2 id="docker-compose-示例"><a href="#docker-compose-示例" class="headerlink" title="docker-compose 示例"></a>docker-compose 示例</h2><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line">version: "3"</span><br><span class="line">services:</span><br><span class="line">    gitlab:</span><br><span class="line">        image: 'gitlab/gitlab-ce:latest'</span><br><span class="line">        restart: always</span><br><span class="line">        hostname: 'gitlab.michael.com'</span><br><span class="line">        container_name: 'devops-gitlab'</span><br><span class="line">        ports:</span><br><span class="line">            - '81:80'</span><br><span class="line">            - '443:443'</span><br><span class="line">            - '23:22'</span><br><span class="line">        environment:</span><br><span class="line">            GITLAB_OMNIBUS_CONFIG: |</span><br><span class="line">                external_url "http://gitlab.michael.com"</span><br><span class="line">                gitlab_rails['gitlab_shell_ssh_port'] = 23</span><br><span class="line">                gitlab_rails['time_zone'] = 'Asia/Shanghai'</span><br><span class="line">                # email setting</span><br><span class="line">                gitlab_rails['smtp_enable'] = true</span><br><span class="line">                gitlab_rails['smtp_address'] = "smtp.163.com"</span><br><span class="line">                gitlab_rails['smtp_port'] = 25</span><br><span class="line">                gitlab_rails['smtp_user_name'] = "michael_show728@163.com"</span><br><span class="line">                gitlab_rails['smtp_password'] = "xxx"</span><br><span class="line">                gitlab_rails['smtp_domain'] = "163.com"</span><br><span class="line">                gitlab_rails['smtp_authentication'] = "login"</span><br><span class="line">                gitlab_rails['smtp_enable_starttls_auto'] = true</span><br><span class="line">                gitlab_rails['gitlab_email_from'] = "michael_show728@163.com"</span><br><span class="line">                gitlab_rails['smtp_tls'] = false</span><br><span class="line">                gitlab_rails['smtp_openssl_verify_mode'] = 'peer'</span><br><span class="line">                user["git_user_email"] = "michael_show728@163.com"</span><br><span class="line">        volumes:</span><br><span class="line">            - '/data/gitlab/config:/etc/gitlab'</span><br><span class="line">            - '/data/gitlab/logs:/var/log/gitlab'</span><br><span class="line">            - '/data/gitlab/data:/var/opt/gitlab'</span><br><span class="line">        networks:</span><br><span class="line">            - gitlab_net</span><br><span class="line"></span><br><span class="line">networks:</span><br><span class="line">    gitlab_net:</span><br><span class="line">        name: gitlab_net</span><br></pre></td></tr></table></figure>
<p>为了在我机器上 <code>192.168.3.66</code> 能够识别这个域名，我通过配置 <code>hosts</code> 文件，将容器的宿主机 IP 和这个域名做了映射：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">192.168.3.43 gitlab.michael.com</span><br></pre></td></tr></table></figure>
<div class="note info no-icon">
            <p>经过试验，挂卷的目录，即宿主机的目录，如果没有创建的话，默认会创建的。有时候没有配置文件是没关系的，但是有时候却会有影响，比如 Nginx 的配置文件你没有事先归档在宿主机的目录上，这时候启动 Nginx 就不会起作用。</p><p>这里启动 gitlab-ce 没有问题，所以，无须利用 docker cp 的方式，预先拷贝一些配置文件出来。</p>
          </div>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">$</span> docker exec -it devops-gitlab bash</span><br><span class="line">root@gitlab:/# cat /etc/hosts</span><br><span class="line">127.0.0.1       localhost</span><br><span class="line">::1     localhost ip6-localhost ip6-loopback</span><br><span class="line">fe00::0 ip6-localnet</span><br><span class="line">ff00::0 ip6-mcastprefix</span><br><span class="line">ff02::1 ip6-allnodes</span><br><span class="line">ff02::2 ip6-allrouters</span><br><span class="line">192.168.240.2   gitlab.michael.com gitlab</span><br></pre></td></tr></table></figure>
<p>可以看到，在容器网络中，gitlab.michael.com 这个域名对应的 IP 是 <code>192.168.240.2</code>。我在局域网里用我自己另一台机器 ping 这个 IP，是不通的。因为我在家是没有 DNS 域名服务器的。这个 IP 很关键，在后面的配置中起到了关键作用。</p>
<h2 id="配置详解"><a href="#配置详解" class="headerlink" title="配置详解"></a>配置详解</h2><h3 id="ports"><a href="#ports" class="headerlink" title="ports"></a>ports</h3><p>端口 <code>81:80</code> 的配置是因为我容器的宿主机的 80 端口是给 Nginx 用了。在同一台机器上，为了不引起端口冲突，将 81 端口和容器的 80 端口映射。 访问 gitlab 服务下面地址皆可：</p>
<ul>
<li><code>宿主机 IP:81</code>，即 <code>192.168.3.43:81</code></li>
<li><code>gitlab.michael.com:81</code></li>
</ul>
<p>这样的网址并不优美，因此，我在 Nginx 中增加了一个 <code>gitlab.conf</code> 的配置文件，这样就可以直接使用 <code>gitlab.michael.com</code> 访问了：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line">upstream  gitlab&#123;</span><br><span class="line">    # 域名对应 gitlab配置中的 external_url</span><br><span class="line">    # 端口对应 gitlab 配置中的 nginx['listen_port']，通过环境变量可设置</span><br><span class="line">    server 192.168.3.43:81;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">server&#123;</span><br><span class="line">    listen 80;</span><br><span class="line">    # 此域名是提供给最终用户的访问地址</span><br><span class="line">    server_name gitlab.michael.com;</span><br><span class="line"></span><br><span class="line">    location / &#123;</span><br><span class="line">        # 这个大小的设置非常重要，如果 git 版本库里面有大文件，设置的太小，文件push 会失败，根据情况调整</span><br><span class="line">        client_max_body_size 50m;</span><br><span class="line"></span><br><span class="line">        proxy_redirect off;</span><br><span class="line">        #以下确保 gitlab中项目的 url 是域名而不是 http://git，不可缺少</span><br><span class="line">        proxy_set_header Host $host;</span><br><span class="line">        proxy_set_header X-Real-IP $remote_addr;</span><br><span class="line">        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;</span><br><span class="line">        # 反向代理到 gitlab 内置的 nginx</span><br><span class="line">        proxy_pass http://gitlab;</span><br><span class="line">        index index.html index.htm;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>关于使用容器搭建 Nginx 可参考之前的文章：<a href="https://michael728.github.io/2019/06/02/docker-create-develop-environment/">使用容器 Docker 创建开发环境</a></p>
<p>端口 <code>&#39;23:22&#39;</code> 表示将宿主机的端口 23 和容器的端口 22 映射。ssh 默认端口就是 22，我们使用 ssh 协议登录宿主机时，就是用的 22 端口。因此，如果 <code>22:22</code> 这种映射的话，会影响登录宿主机的。也有文章修改了宿主机的 sshd 默认端口，但是我没有这么做。有需要的话，可以查看<a href="https://juejin.im/post/5a4c9ff36fb9a04507700fcc" target="_blank" rel="noopener">通过 docker 搭建自用的 gitlab 服务</a></p>
<p><code>gitlab_rails[&#39;gitlab_shell_ssh_port&#39;] = 23</code> 这个环境变量的设置很关键，当我们选择 ssh 协议方式克隆代码库时，复制的地址将会自动变为：<code>ssh://git@gitlab.michael.com:23/root/gitlab-demo.git</code></p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#</span> michael @ Michael-MBP in ~/Code/00-Temp [15:25:21]</span><br><span class="line"><span class="meta">$</span> git clone ssh://git@gitlab.michael.com:23/root/gitlab-demo.git</span><br><span class="line">Cloning into 'gitlab-demo'...</span><br><span class="line">remote: Enumerating objects: 6, done.</span><br><span class="line">remote: Counting objects: 100% (6/6), done.</span><br><span class="line">remote: Compressing objects: 100% (2/2), done.</span><br><span class="line">remote: Total 6 (delta 0), reused 0 (delta 0)</span><br><span class="line">Receiving objects: 100% (6/6), done.</span><br></pre></td></tr></table></figure>
<h3 id="external-url"><a href="#external-url" class="headerlink" title="external_url"></a>external_url</h3><p>这里的 <code>external_url</code> 配置的是外部 URL，会影响项目的访问地址，如果不配置，项目的访问地址会是一个随机字符串，在云服务器上搭建时尤其要注意这一点。</p>
<p>例如，新建项目的 HTTP 地址为：<code>http://gitlab.michael.com/root/gitlab-demo</code>，这里的地址中的 <code>gitlab.michael.com</code> 就是我在 <code>docker-compose.yml</code> 文件中配置的环境变量。</p>
<div class="note success no-icon">
            <p>我发现仅配置了 <code>hostname</code> 为域名时，外部 URL 默认和这个域名保持了一致，也实现了 <code>external_url</code> 的作用。</p>
          </div>
<h3 id="开启邮件服务"><a href="#开启邮件服务" class="headerlink" title="开启邮件服务"></a>开启邮件服务</h3><p>我们在使用 github 等类似的平台工具的时候都会用到邮件服务，比如你在 github 上进行注册、密码重置、有人给你的开源项目提 issue 等等的时候，你通常都会收到邮件提醒。 gitlab 肯定也会有这个功能的，下面我们就来开启这个功能，在此之前需要准备一个邮箱账号，这个账号是用来负责发送邮件的，需要开启 smtp 协议支持。本文以 163 邮箱为例。</p>
<p>说明：</p>
<ul>
<li><code>gitlab_rails[&#39;smtp_address&#39;]</code> ：SMTP服务地址，不同的服务商不同</li>
<li><code>gitlab_rails[&#39;smtp_port&#39;]</code> ：服务端口</li>
<li><code>gitlab_rails[&#39;smtp_user_name&#39;]</code> ：用户名，自己注册的</li>
<li><code>gitlab_rails[&#39;smtp_password&#39;]</code> ：客户端授权秘钥（获取方式，下图讲解）</li>
<li><code>gitlab_rails[&#39;gitlab_email_from&#39;]</code> ：发出邮件的用户，注意跟用户名保持一致</li>
<li><code>user[&quot;git_user_email&quot;]</code> ：发出用户，注意跟用户名保持一致</li>
</ul>
<p>亲测，当你使用管理员账户登录时，添加用户之后，上面的配置能够成功发送邮件：</p>
<p><img src="https://ws1.sinaimg.cn/mw690/6d9475f6ly1g41uqkxowbj20ze0b775y.jpg" alt></p>
<p><img src="https://ws1.sinaimg.cn/mw690/6d9475f6ly1g41w79zw81j20p10ddq4x.jpg" alt></p>
<p>了解更多，查看 <a href="https://docs.gitlab.com/omnibus/settings/smtp.html" target="_blank" rel="noopener">官网 SMTP 设置</a></p>
<h3 id="数据持久化"><a href="#数据持久化" class="headerlink" title="数据持久化"></a>数据持久化</h3><p>通过挂卷的方式，将 gitlab 的相关数据保存到宿主机上：</p>
<table>
<thead>
<tr>
<th>当地的位置</th>
<th>容器的位置</th>
<th>作用</th>
</tr>
</thead>
<tbody>
<tr>
<td>/data/gitlab/config</td>
<td>/etc/gitlab</td>
<td>用于存储GitLab配置文件</td>
</tr>
<tr>
<td>/data/gitlab/logs</td>
<td>/var/log/gitlab</td>
<td>用于存储日志</td>
</tr>
<tr>
<td>/data/gitlab/data</td>
<td>/var/opt/gitlab</td>
<td>用于存储应用数据</td>
</tr>
</tbody>
</table>
<h2 id="gitlab-runner"><a href="#gitlab-runner" class="headerlink" title="gitlab-runner"></a>gitlab-runner</h2><p>dockerhub 中有<a href="https://hub.docker.com/r/gitlab/gitlab-runner/tags" target="_blank" rel="noopener">gitlab/gitlab-runner</a>的景象。</p>
<p>Runner 的配置文件是 <code>/etc/gitlab-runner/config.toml</code>，它的字段说明，可以查看文档：</p>
<ul>
<li><a href="https://gitlab.com/gitlab-org/gitlab-runner/blob/master/docs/configuration/advanced-configuration.md" target="_blank" rel="noopener">Gitlab-runner Advanced configuration</a></li>
</ul>
<h3 id="创建-runner-容器"><a href="#创建-runner-容器" class="headerlink" title="创建 runner 容器"></a>创建 runner 容器</h3><p>先这种方式运行一下，将配置文件拷贝出来，之后再通过 docker-compose 的方式启动：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">mkdir -p /data/gitlab-runner</span><br><span class="line">docker run -d --name gitlab-runner --restart always \</span><br><span class="line">   -v /var/run/docker.sock:/var/run/docker.sock \</span><br><span class="line">   --network=gitlab_net \</span><br><span class="line">   --add-host=gitlab.michael.com:192.168.240.2 \</span><br><span class="line">   gitlab/gitlab-runner:latest</span><br><span class="line">docker cp gitlab-runner:/etc/gitlab-runner /data/gitlab-runner/config</span><br></pre></td></tr></table></figure>
<p>上面这种方式，通过 <code>network</code> 的方式，将 runner 和 gitlab server 机器处于同一网络下。所以，当在 runner 中配置 host 之后，IP 就是可达的了。域名会被解析到。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">$</span> docker exec -it gitlab-runner bash</span><br><span class="line">root@55fc04a0d51a:/# cat /etc/hosts</span><br><span class="line">127.0.0.1       localhost</span><br><span class="line">::1     localhost ip6-localhost ip6-loopback</span><br><span class="line">fe00::0 ip6-localnet</span><br><span class="line">ff00::0 ip6-mcastprefix</span><br><span class="line">ff02::1 ip6-allnodes</span><br><span class="line">ff02::2 ip6-allrouters</span><br><span class="line">192.168.240.2    gitlab.michael.com</span><br><span class="line">192.168.240.3   55fc04a0d51a</span><br></pre></td></tr></table></figure>
<p>将之前的 runner 容器停止：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">docker stop gitlab-runner</span><br><span class="line">docker rm gitlab-runner</span><br></pre></td></tr></table></figure>
<p>docker-compose.yml 示例：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">version: "2.1"</span><br><span class="line">services:</span><br><span class="line">  runner:</span><br><span class="line">    image: 'gitlab/gitlab-runner:latest'</span><br><span class="line">    container_name: 'gitlab-runner'</span><br><span class="line">    restart: always</span><br><span class="line">    volumes:</span><br><span class="line">      - '/data/gitlab-runner/config:/etc/gitlab-runner'</span><br><span class="line">      - '/var/run/docker.sock:/var/run/docker.sock'</span><br><span class="line">    networks:</span><br><span class="line">      - gitlab_net</span><br><span class="line">    extra_hosts:</span><br><span class="line">      - "gitlab.michael.com:192.168.240.2"</span><br><span class="line"></span><br><span class="line">networks:</span><br><span class="line">  gitlab_net:</span><br><span class="line">    external: true</span><br></pre></td></tr></table></figure>
<h3 id="获取-Runner-信息"><a href="#获取-Runner-信息" class="headerlink" title="获取 Runner 信息"></a>获取 Runner 信息</h3><p>gitlab-runner 容器正常运行之后，注册到 gitlab 上的方式有两处：</p>
<ul>
<li>管理员从 管理中心-Runner 中，注册时使用这里的 <code>shared runner token</code>，这种方式添加的 Runner 将作为 <code>shared Runner</code></li>
<li>进入具体的项目 设置-CI/CD-Runner-专用 Runner，这种方式添加的 Runner 将作为 <code>专用 Runner</code></li>
</ul>
<p><img src="https://ws1.sinaimg.cn/mw690/6d9475f6ly1g41yzy7i10j20z50e741d.jpg" alt></p>
<h3 id="注册-Runner"><a href="#注册-Runner" class="headerlink" title="注册 Runner"></a>注册 Runner</h3><p>注册 Runner 时，<a href="https://docs.gitlab.com/runner/executors/README.html" target="_blank" rel="noopener">关于 excutor</a> 有好几种类型可以选择，我尝试了 shell 和 docker 两种类型。</p>
<h4 id="shell"><a href="#shell" class="headerlink" title="shell"></a>shell</h4><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">$</span> docker exec -it gitlab-runner gitlab-runner register</span><br><span class="line">Runtime platform                                    arch=amd64 os=linux pid=32 revision=ac2a293c version=11.11.2</span><br><span class="line">Running in system-mode.</span><br><span class="line"></span><br><span class="line">Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com/):</span><br><span class="line">http://gitlab.michael.com/</span><br><span class="line">Please enter the gitlab-ci token for this runner:</span><br><span class="line">Wt3GuGq1nAdKWaeBrGM7</span><br><span class="line">Please enter the gitlab-ci description for this runner:</span><br><span class="line">[8bc594b5f4f4]: shared runner</span><br><span class="line">Please enter the gitlab-ci tags for this runner (comma separated):</span><br><span class="line">centos,shared</span><br><span class="line">Registering runner... succeeded                     runner=P6Pme8J5</span><br><span class="line">Please enter the executor: ssh, docker+machine, kubernetes, parallels, docker-ssh, shell, virtualbox, docker-ssh+machine, docker:</span><br><span class="line">shell</span><br><span class="line">Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!</span><br></pre></td></tr></table></figure>
<h4 id="docker"><a href="#docker" class="headerlink" title="docker"></a>docker</h4><p>添加共享 runner：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">$</span> docker exec -it gitlab-runner gitlab-runner register</span><br><span class="line">Runtime platform                                    arch=amd64 os=linux pid=31 revision=ac2a293c version=11.11.2</span><br><span class="line">Running in system-mode.</span><br><span class="line"></span><br><span class="line">Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com/):</span><br><span class="line">http://gitlab.michael.com/</span><br><span class="line">Please enter the gitlab-ci token for this runner:</span><br><span class="line">5SSZ_QcjnLFv9s36ALsL</span><br><span class="line">Please enter the gitlab-ci description for this runner:</span><br><span class="line">[7e5c74d87239]:</span><br><span class="line">Please enter the gitlab-ci tags for this runner (comma separated):</span><br><span class="line">docker</span><br><span class="line">Registering runner... succeeded                     runner=5SSZ_Qcj</span><br><span class="line">Please enter the executor: ssh, virtualbox, docker+machine, docker-ssh+machine, kubernetes, docker-ssh, parallels, shell, docker:</span><br><span class="line">docker</span><br><span class="line">Please enter the default Docker image (e.g. ruby:2.1):</span><br><span class="line">alpine:latest</span><br><span class="line">Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!</span><br></pre></td></tr></table></figure>
<p>选择 docker 为 excutor 时，config.toml 内容配置成这样，实现了网络的畅通：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">$</span> cat config.toml</span><br><span class="line">concurrent = 1</span><br><span class="line">check_interval = 0</span><br><span class="line"></span><br><span class="line">[session_server]</span><br><span class="line">  session_timeout = 1800</span><br><span class="line"></span><br><span class="line">[[runners]]</span><br><span class="line">  name = "michael demo"</span><br><span class="line">  url = "http://gitlab.michael.com/"</span><br><span class="line">  token = "Wt3GuGq1nAdKWaeBrGM7"</span><br><span class="line">  executor = "docker"</span><br><span class="line">  [runners.custom_build_dir]</span><br><span class="line">  [runners.docker]</span><br><span class="line">    tls_verify = false</span><br><span class="line">    image = "alpine:latest"</span><br><span class="line">    extra_hosts = ["gitlab.michael.com:192.168.240.2"]</span><br><span class="line">    network_mode = "gitlab_net"</span><br><span class="line">    privileged = true</span><br><span class="line">    disable_entrypoint_overwrite = false</span><br><span class="line">    oom_kill_disable = false</span><br><span class="line">    disable_cache = false</span><br><span class="line">    volumes = ["/cache"]</span><br><span class="line">    shm_size = 0</span><br><span class="line">  [runners.cache]</span><br><span class="line">    [runners.cache.s3]</span><br><span class="line">    [runners.cache.gcs]</span><br></pre></td></tr></table></figure>
<p>主要就是添加了网络配置：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">extra_hosts = ["gitlab.michael.com:192.168.240.2"]</span><br><span class="line">network_mode = "gitlab_net"</span><br></pre></td></tr></table></figure>
<blockquote>
<p>gitlab-runner 虽然是基于 docker 安装的，但是它每个 stage 执行是由 runner 在主机另起一个 docker 容器来执行的，执行完毕后自动销毁，并不是在 gitlab-runner 容器内部启动新的容器。 runner 相当于一个在主机的 agent 程序，来负责接收任务、分发并创建新的容器执行任务，最后回传执行结果到 gitlab。</p>
</blockquote>
<h3 id="设置-runner"><a href="#设置-runner" class="headerlink" title="设置 runner"></a>设置 runner</h3><p>这时候，我们可以在「管理中心」的 Runner 中看到所有的 Runner 信息。</p>
<div class="note warning no-icon">
            <p>注意点：在同一个 git-runner 容器中，注册过之后，重置了 token，上一个 token 就会失效。尽管在管理中心会看到两个 runner，但是通过查看 git-runner 日志，其中有一个是失效的。</p><p>此外，我一开始注册之后，流水线始终提示缺少 runner。很奇怪，因为去 Runner 界面查看，是 <code>available</code> 状态的。后来去管理中心看了一下，原来，新创建的runner，默认情况下，只对打 tag 的 commit 触发任务，否则会找不到对应 tag 的机器！除非，你勾选上无需 tag。</p>
          </div>
<p><img src="https://ws1.sinaimg.cn/mw690/6d9475f6ly1g4221cdnkqj20yw0iaacy.jpg" alt></p>
<p>此外，同一个 gitlab-runner 我分别注册了两次，设置的 tag 不一样。使用的是项目的 token 和 共享的 token，二者皆是有效的。这时候注册的时候，会显示两个 runner，二者都会被成功调用。</p>
<h2 id="CI-CD"><a href="#CI-CD" class="headerlink" title="CI/CD"></a>CI/CD</h2><p>关于 gitlab 的 CI/CD 功能，更多可以查看 <a href="https://docs.gitlab.com/ee/ci/yaml/README.html" target="_blank" rel="noopener">GitLab Documentation GitLab CI/CD GitLab CI/CD Pipeline Configuration Reference</a>。</p>
<h3 id="流水线"><a href="#流水线" class="headerlink" title="流水线"></a>流水线</h3><p>要使用流水线功能，前提是 repo 中要有 <code>.gitlab-ci.yml</code> 文件。</p>
<p>一个示例：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br></pre></td><td class="code"><pre><span class="line">stages:</span><br><span class="line">  - install_deps</span><br><span class="line">  - test</span><br><span class="line">  - build</span><br><span class="line">  - deploy_test</span><br><span class="line">  - deploy_production</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span> 安装依赖</span><br><span class="line">install_deps:</span><br><span class="line">  stage: install_deps</span><br><span class="line">  only:</span><br><span class="line">    - develop</span><br><span class="line">    - master</span><br><span class="line">  script:</span><br><span class="line">    - pwd</span><br><span class="line">    - echo "now install_deps stage"</span><br><span class="line">  tags:</span><br><span class="line">  - docker</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span> 运行测试用例</span><br><span class="line">test:</span><br><span class="line">  stage: test</span><br><span class="line">  only:</span><br><span class="line">    - develop</span><br><span class="line">    - master</span><br><span class="line">  script:</span><br><span class="line">    - pwd</span><br><span class="line">    - echo "now test stage"</span><br><span class="line">  tags:</span><br><span class="line">  - shell</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span> 编译</span><br><span class="line">build:</span><br><span class="line">  stage: build</span><br><span class="line">  only:</span><br><span class="line">    - develop</span><br><span class="line">    - master</span><br><span class="line">  script:</span><br><span class="line">    - pwd</span><br><span class="line">    - echo "now build stage 1"</span><br><span class="line">  tags:</span><br><span class="line">  - docker</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span> 部署测试服务器</span><br><span class="line">deploy_test:</span><br><span class="line">  stage: deploy_test</span><br><span class="line">  only:</span><br><span class="line">    - develop</span><br><span class="line">  script:</span><br><span class="line">    - echo "now deploy_test stage"</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span> 部署生产服务器</span><br><span class="line">deploy_production:</span><br><span class="line">  stage: deploy_production</span><br><span class="line">  only:</span><br><span class="line">    - master</span><br><span class="line">  script:</span><br><span class="line">    - echo "now deploy_production stage"</span><br></pre></td></tr></table></figure>
<p><img src="https://ws1.sinaimg.cn/mw690/6d9475f6ly1g421vw7in7j21jp0ekwgk.jpg" alt></p>
<h2 id="参考"><a href="#参考" class="headerlink" title="参考"></a>参考</h2><p>gitlab-ce</p>
<ul>
<li><a href="https://docs.gitlab.com/omnibus/README.html" target="_blank" rel="noopener">Omnibus GitLab documentation</a></li>
<li><a href="https://docs.gitlab.com/omnibus/settings/configuration.html" target="_blank" rel="noopener">Omnibus GitLab documentation Configuring omnibus settings Configuration options</a></li>
<li><a href="https://cnodejs.org/topic/5bb4caa69545eaf107b9c7e6" target="_blank" rel="noopener">cnodejs-使用docker-compose搭建gitlab</a> 挺全的，还介绍了 gitlab runner 的使用，推荐</li>
<li><a href="https://juejin.im/post/5a4c9ff36fb9a04507700fcc" target="_blank" rel="noopener">掘金-通过 docker 搭建自用的 gitlab 服务</a></li>
<li><a href="https://segmentfault.com/a/1190000014305359" target="_blank" rel="noopener">segmentfault-使用docker搭建gitlab环境</a> 邮箱配置参考</li>
</ul>
<p>gitlab-runner</p>
<ul>
<li><a href="https://docs.gitlab.com/runner/install/docker.html" target="_blank" rel="noopener">Run GitLab Runner in a container</a></li>
<li><a href="https://www.funtl.com/zh/apache-dubbo-ci/%E5%9F%BA%E4%BA%8E-Docker-%E5%AE%89%E8%A3%85-GitLab-Runner.html" target="_blank" rel="noopener">千峰教育-基于 Docker 安装 GitLab Runner</a></li>
<li><a href="https://gitlab.com/help/ci/docker/using_docker_build.md" target="_blank" rel="noopener">Building Docker images with GitLab CI/CD</a> 利用 Gitlab CI/CD 构建 Docker 镜像，可以查看</li>
<li><a href="https://blog.csdn.net/aixiaoyang168/article/details/72168834" target="_blank" rel="noopener">Docker搭建自己的Gitlab CI Runner</a></li>
</ul>
<p>CI/CD</p>
<ul>
<li><a href="https://docs.gitlab.com/ee/ci/yaml/README.html" target="_blank" rel="noopener">GitLab Documentation GitLab CI/CD GitLab CI/CD Pipeline Configuration Reference</a></li>
</ul>

      
    </div>

    
      

  <div class="popular-posts-header">相关文章</div>
  <ul class="popular-posts">
  
    <li class="popular-posts-item">
      
      
      <div class="popular-posts-title"><a href="/2019/04/23/linux-lvm-partion/" rel="bookmark">Linux lvm 分区知识笔记</a></div>
      
    </li>
  
    <li class="popular-posts-item">
      
      
      <div class="popular-posts-title"><a href="/2016/05/03/SQL/" rel="bookmark">SQL基础语法笔记教程整理</a></div>
      
    </li>
  
    <li class="popular-posts-item">
      
      
      <div class="popular-posts-title"><a href="/2018/09/16/blog-interesting/" rel="bookmark">那些有趣的博客</a></div>
      
    </li>
  
  </ul>


    

    
    
    

    
      <div id="wechat_subscriber" style="display: block; padding: 10px 0; margin: 20px auto; width: 100%; text-align: center;">
  <img id="wechat_subscriber_qcode" src="/images/wechat-qcode.jpg" alt="Michael翔 wechat" style="width: 200px; max-width: 100%;">
  <div>欢迎订阅 ヾﾉ≧∀≦)o</div>
</div>

    

    
      
    
    
      <div>
        <div id="reward-container">
  <div>我知道是不会有人点的，但万一有人想不开呢👇</div>
  <button id="reward-button" disable="enable" onclick="var qr = document.getElementById(&quot;qr&quot;); qr.style.display = (qr.style.display === 'none') ? 'block' : 'none';">
    打赏
  </button>
  <div id="qr" style="display: none;">

    
      
      
        
      
      <div style="display: inline-block">
        <img src="/images/wechatpay.jpg" alt="Michael翔 微信支付">
        <p>微信支付</p>
      </div>
    
      
      
        
      
      <div style="display: inline-block">
        <img src="/images/alipay.png" alt="Michael翔 支付宝">
        <p>支付宝</p>
      </div>
    

  </div>
</div>

      </div>
    

    
      <div>
        




  



<ul class="post-copyright">
  <li class="post-copyright-author">
    <strong>本文作者： </strong>Michael翔</li>
  <li class="post-copyright-link">
    <strong>本文链接：</strong>
    
    <a href="https://michael728.github.io/2019/06/15/docker-compose-install-gitlab-runner/" title="使用 Dockcer-Compose 安装 Gitlab 服务">https://michael728.github.io/2019/06/15/docker-compose-install-gitlab-runner/</a>
  </li>
  <li class="post-copyright-license">
    <strong>版权声明： </strong>本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" rel="noopener" target="_blank"><i class="fa fa-fw fa-creative-commons"></i>BY-NC-SA</a> 许可协议。转载请注明出处！</li>
</ul>

      </div>
    

    <footer class="post-footer">
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2019/06/15/docker-compose-networks/" rel="next" title="Docker Compose 方式下的容器网络基础知识点">
                <i class="fa fa-chevron-left"></i> Docker Compose 方式下的容器网络基础知识点
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2019/06/16/cicd-hexo-blog-travis/" rel="prev" title="Github 使用 Travis CI 实现 Hexo 博客自动部署">
                Github 使用 Travis CI 实现 Hexo 博客自动部署 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>


  </div>


          </div>
          

  
    <div class="comments" id="comments">
      <div id="lv-container" data-id="city" data-uid="MTAyMC8yODgyMC81Mzkw" "></div>
    </div>

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image" src="/images/logo.jpg" alt="Michael翔">
            
              <p class="site-author-name" itemprop="name">Michael翔</p>
              <div class="site-description motion-element" itemprop="description">DevOps CICD Blog 持续学习</div>
          </div>

          
            <nav class="site-state motion-element">
              
                <div class="site-state-item site-state-posts">
                
                  <a href="/archives/">
                
                    <span class="site-state-item-count">82</span>
                    <span class="site-state-item-name">日志</span>
                  </a>
                </div>
              

              
                
                
                <div class="site-state-item site-state-categories">
                  
                    
                      <a href="/categories/">
                    
                  
                    
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                    <span class="site-state-item-count">19</span>
                    <span class="site-state-item-name">分类</span>
                  </a>
                </div>
              

              
                
                
                <div class="site-state-item site-state-tags">
                  
                    
                      <a href="/tags/">
                    
                  
                    
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                    <span class="site-state-item-count">95</span>
                    <span class="site-state-item-name">标签</span>
                  </a>
                </div>
              
            </nav>
          

          
            <div class="feed-link motion-element">
              <a href="/atom.xml" rel="alternate">
                <i class="fa fa-rss"></i>
                RSS
              </a>
            </div>
          

          

          
            <div class="links-of-author motion-element">
              
                <span class="links-of-author-item">
                  
                  
                    
                  
                  
                    
                  
                  <a href="https://github.com/Michael728" title="GitHub &rarr; https://github.com/Michael728" rel="noopener" target="_blank"><i class="fa fa-fw fa-github"></i>GitHub</a>
                </span>
              
                <span class="links-of-author-item">
                  
                  
                    
                  
                  
                    
                  
                  <a href="https://weibo.com/1838446070/profile?topnav=1&wvr=6" title="微博 &rarr; https://weibo.com/1838446070/profile?topnav=1&wvr=6" rel="noopener" target="_blank"><i class="fa fa-fw fa-weibo"></i>微博</a>
                </span>
              
                <span class="links-of-author-item">
                  
                  
                    
                  
                  
                    
                  
                  <a href="https://www.cnblogs.com/michael-xiang/" title="博客园 &rarr; https://www.cnblogs.com/michael-xiang/" rel="noopener" target="_blank"><i class="fa fa-fw fa-globe"></i>博客园</a>
                </span>
              
                <span class="links-of-author-item">
                  
                  
                    
                  
                  
                    
                  
                  <a href="https://www.zhihu.com/people/michaelXoX" title="知乎 &rarr; https://www.zhihu.com/people/michaelXoX" rel="noopener" target="_blank"><i class="fa fa-fw fa-globe"></i>知乎</a>
                </span>
              
                <span class="links-of-author-item">
                  
                  
                    
                  
                  
                    
                  
                  <a href="https://twitter.com/728_michael" title="Twitter &rarr; https://twitter.com/728_michael" rel="noopener" target="_blank"><i class="fa fa-fw fa-twitter"></i>Twitter</a>
                </span>
              
                <span class="links-of-author-item">
                  
                  
                    
                  
                  
                    
                  
                  <a href="https://t.me/michaelxiang" title="Telegram &rarr; https://t.me/michaelxiang" rel="noopener" target="_blank"><i class="fa fa-fw fa-telegram"></i>Telegram</a>
                </span>
              
            </div>
          

          
             <div class="cc-license motion-element" itemprop="license">
              
              
                
              
              
              
              <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" class="cc-opacity" rel="noopener" target="_blank"><img src="/images/cc-by-nc-sa.svg" alt="Creative Commons"></a>
             </div>
          

          
          
            <div class="links-of-blogroll motion-element links-of-blogroll-block">
              <div class="links-of-blogroll-title">
                <i class="fa  fa-fw fa-link"></i>
                关注列表
              </div>
              <ul class="links-of-blogroll-list">
                
                  <li class="links-of-blogroll-item">
                    <a href="https://michael728.github.io/2018/09/16/blog-interesting/" title="https://michael728.github.io/2018/09/16/blog-interesting/">Blog List</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://zcheng.ren/posts/" title="https://zcheng.ren/posts/" rel="noopener" target="_blank">ZeeCoder</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://ehlxr.me/" title="https://ehlxr.me/" rel="noopener" target="_blank">ehlxr</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.kawabangga.com/" title="https://www.kawabangga.com/" rel="noopener" target="_blank">卡瓦邦噶-蚂蚁SRE</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://blog.stdioa.com/" title="https://blog.stdioa.com/" rel="noopener" target="_blank">David Dai</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://bestzuo.cn/" title="https://bestzuo.cn/" rel="noopener" target="_blank">Sanarous</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.zhyong.cn/" title="https://www.zhyong.cn/" rel="noopener" target="_blank">ZhangYong-爱折腾软件</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.dalaoyang.cn/" title="https://www.dalaoyang.cn/" rel="noopener" target="_blank">Dalaoyang-SpringBoot</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="http://www.wuliaole.com/index.html" title="http://www.wuliaole.com/index.html" rel="noopener" target="_blank">无聊小博</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="http://ilovey.live/" title="http://ilovey.live/" rel="noopener" target="_blank">低调小熊猫-Java开发</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://michael728.github.io/2016/09/03/blog-logfile/" title="https://michael728.github.io/2016/09/03/blog-logfile/">建站日志</a>
                  </li>
                
              </ul>
            </div>
          

          
            
          
          

        </div>
      </div>

      
      <!--noindex-->
        <div class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
            
            
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#gitlab-镜像"><span class="nav-number">1.</span> <span class="nav-text">gitlab 镜像</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#硬件要求"><span class="nav-number">2.</span> <span class="nav-text">硬件要求</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#CPU"><span class="nav-number">2.1.</span> <span class="nav-text">CPU</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Memory"><span class="nav-number">2.2.</span> <span class="nav-text">Memory</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#docker-compose-示例"><span class="nav-number">3.</span> <span class="nav-text">docker-compose 示例</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#配置详解"><span class="nav-number">4.</span> <span class="nav-text">配置详解</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#ports"><span class="nav-number">4.1.</span> <span class="nav-text">ports</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#external-url"><span class="nav-number">4.2.</span> <span class="nav-text">external_url</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#开启邮件服务"><span class="nav-number">4.3.</span> <span class="nav-text">开启邮件服务</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#数据持久化"><span class="nav-number">4.4.</span> <span class="nav-text">数据持久化</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#gitlab-runner"><span class="nav-number">5.</span> <span class="nav-text">gitlab-runner</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#创建-runner-容器"><span class="nav-number">5.1.</span> <span class="nav-text">创建 runner 容器</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#获取-Runner-信息"><span class="nav-number">5.2.</span> <span class="nav-text">获取 Runner 信息</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#注册-Runner"><span class="nav-number">5.3.</span> <span class="nav-text">注册 Runner</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#shell"><span class="nav-number">5.3.1.</span> <span class="nav-text">shell</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#docker"><span class="nav-number">5.3.2.</span> <span class="nav-text">docker</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#设置-runner"><span class="nav-number">5.4.</span> <span class="nav-text">设置 runner</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#CI-CD"><span class="nav-number">6.</span> <span class="nav-text">CI/CD</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#流水线"><span class="nav-number">6.1.</span> <span class="nav-text">流水线</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#参考"><span class="nav-number">7.</span> <span class="nav-text">参考</span></a></li></ol></div>
            

          </div>
        </div>
      <!--/noindex-->
      

      
        <div class="back-to-top">
          <i class="fa fa-arrow-up"></i>
          
            <span id="scrollpercent"><span>0</span>%</span>
          
        </div>
      

    </div>
  </aside>
  


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; 2015 – <span itemprop="copyrightYear">2020</span>
  <span class="with-love" id="animate">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Michael翔</span>

  

  
</div>









        








        
      </div>
    </footer>

    

    

    

    
  </div>

  

<script>
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>












  



  
    
    
      
    
  
  <script color="0,0,255" opacity="0.5" zindex="-1" count="99" src="//cdn.jsdelivr.net/gh/theme-next/theme-next-canvas-nest@1.0.0/canvas-nest.min.js"></script>













  
  <script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>

  
  <script src="/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script src="//cdnjs.cloudflare.com/ajax/libs/fancybox/3.5.6/jquery.fancybox.min.js"></script>


  


  <script src="/js/utils.js?v=7.1.1"></script>

  <script src="/js/motion.js?v=7.1.1"></script>



  
  


  <script src="/js/affix.js?v=7.1.1"></script>

  <script src="/js/schemes/pisces.js?v=7.1.1"></script>




  
  <script src="/js/scrollspy.js?v=7.1.1"></script>
<script src="/js/post-details.js?v=7.1.1"></script>



  


  <script src="/js/next-boot.js?v=7.1.1"></script>


  

  

  

  


  
    <script>
  window.livereOptions = {
    refer: '2019/06/15/docker-compose-install-gitlab-runner/'
  };
  (function(d, s) {
    var j, e = d.getElementsByTagName(s)[0];
    if (typeof LivereTower === 'function') { return; }
    j = d.createElement(s);
    j.src = 'https://cdn-city.livere.com/js/embed.dist.js';
    j.async = true;
    e.parentNode.insertBefore(j, e);
  })(document, 'script');
</script>

  


  
  <script>
    // Popup Window;
    var isfetched = false;
    var isXml = true;
    // Search DB path;
    var search_path = "search.xml";
    if (search_path.length === 0) {
      search_path = "search.xml";
    } else if (/json$/i.test(search_path)) {
      isXml = false;
    }
    var path = "/" + search_path;
    // monitor main search box;

    var onPopupClose = function (e) {
      $('.popup').hide();
      $('#local-search-input').val('');
      $('.search-result-list').remove();
      $('#no-result').remove();
      $(".local-search-pop-overlay").remove();
      $('body').css('overflow', '');
    }

    function proceedsearch() {
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay"></div>')
        .css('overflow', 'hidden');
      $('.search-popup-overlay').click(onPopupClose);
      $('.popup').toggle();
      var $localSearchInput = $('#local-search-input');
      $localSearchInput.attr("autocapitalize", "none");
      $localSearchInput.attr("autocorrect", "off");
      $localSearchInput.focus();
    }

    // search function;
    var searchFunc = function(path, search_id, content_id) {
      'use strict';

      // start loading animation
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay">' +
          '<div id="search-loading-icon">' +
          '<i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>' +
          '</div>' +
          '</div>')
        .css('overflow', 'hidden');
      $("#search-loading-icon").css('margin', '20% auto 0 auto').css('text-align', 'center');

      

      $.ajax({
        url: path,
        dataType: isXml ? "xml" : "json",
        async: true,
        success: function(res) {
          // get the contents from search data
          isfetched = true;
          $('.popup').detach().appendTo('.header-inner');
          var datas = isXml ? $("entry", res).map(function() {
            return {
              title: $("title", this).text(),
              content: $("content",this).text(),
              url: $("url" , this).text()
            };
          }).get() : res;
          var input = document.getElementById(search_id);
          var resultContent = document.getElementById(content_id);
          var inputEventFunction = function() {
            var searchText = input.value.trim().toLowerCase();
            var keywords = searchText.split(/[\s\-]+/);
            if (keywords.length > 1) {
              keywords.push(searchText);
            }
            var resultItems = [];
            if (searchText.length > 0) {
              // perform local searching
              datas.forEach(function(data) {
                var isMatch = false;
                var hitCount = 0;
                var searchTextCount = 0;
                var title = data.title.trim();
                var titleInLowerCase = title.toLowerCase();
                var content = data.content.trim().replace(/<[^>]+>/g,"");
                
                var contentInLowerCase = content.toLowerCase();
                var articleUrl = decodeURIComponent(data.url).replace(/\/{2,}/g, '/');
                var indexOfTitle = [];
                var indexOfContent = [];
                // only match articles with not empty titles
                if(title != '') {
                  keywords.forEach(function(keyword) {
                    function getIndexByWord(word, text, caseSensitive) {
                      var wordLen = word.length;
                      if (wordLen === 0) {
                        return [];
                      }
                      var startPosition = 0, position = [], index = [];
                      if (!caseSensitive) {
                        text = text.toLowerCase();
                        word = word.toLowerCase();
                      }
                      while ((position = text.indexOf(word, startPosition)) > -1) {
                        index.push({position: position, word: word});
                        startPosition = position + wordLen;
                      }
                      return index;
                    }

                    indexOfTitle = indexOfTitle.concat(getIndexByWord(keyword, titleInLowerCase, false));
                    indexOfContent = indexOfContent.concat(getIndexByWord(keyword, contentInLowerCase, false));
                  });
                  if (indexOfTitle.length > 0 || indexOfContent.length > 0) {
                    isMatch = true;
                    hitCount = indexOfTitle.length + indexOfContent.length;
                  }
                }

                // show search results

                if (isMatch) {
                  // sort index by position of keyword

                  [indexOfTitle, indexOfContent].forEach(function (index) {
                    index.sort(function (itemLeft, itemRight) {
                      if (itemRight.position !== itemLeft.position) {
                        return itemRight.position - itemLeft.position;
                      } else {
                        return itemLeft.word.length - itemRight.word.length;
                      }
                    });
                  });

                  // merge hits into slices

                  function mergeIntoSlice(text, start, end, index) {
                    var item = index[index.length - 1];
                    var position = item.position;
                    var word = item.word;
                    var hits = [];
                    var searchTextCountInSlice = 0;
                    while (position + word.length <= end && index.length != 0) {
                      if (word === searchText) {
                        searchTextCountInSlice++;
                      }
                      hits.push({position: position, length: word.length});
                      var wordEnd = position + word.length;

                      // move to next position of hit

                      index.pop();
                      while (index.length != 0) {
                        item = index[index.length - 1];
                        position = item.position;
                        word = item.word;
                        if (wordEnd > position) {
                          index.pop();
                        } else {
                          break;
                        }
                      }
                    }
                    searchTextCount += searchTextCountInSlice;
                    return {
                      hits: hits,
                      start: start,
                      end: end,
                      searchTextCount: searchTextCountInSlice
                    };
                  }

                  var slicesOfTitle = [];
                  if (indexOfTitle.length != 0) {
                    slicesOfTitle.push(mergeIntoSlice(title, 0, title.length, indexOfTitle));
                  }

                  var slicesOfContent = [];
                  while (indexOfContent.length != 0) {
                    var item = indexOfContent[indexOfContent.length - 1];
                    var position = item.position;
                    var word = item.word;
                    // cut out 100 characters
                    var start = position - 20;
                    var end = position + 80;
                    if(start < 0){
                      start = 0;
                    }
                    if (end < position + word.length) {
                      end = position + word.length;
                    }
                    if(end > content.length){
                      end = content.length;
                    }
                    slicesOfContent.push(mergeIntoSlice(content, start, end, indexOfContent));
                  }

                  // sort slices in content by search text's count and hits' count

                  slicesOfContent.sort(function (sliceLeft, sliceRight) {
                    if (sliceLeft.searchTextCount !== sliceRight.searchTextCount) {
                      return sliceRight.searchTextCount - sliceLeft.searchTextCount;
                    } else if (sliceLeft.hits.length !== sliceRight.hits.length) {
                      return sliceRight.hits.length - sliceLeft.hits.length;
                    } else {
                      return sliceLeft.start - sliceRight.start;
                    }
                  });

                  // select top N slices in content

                  var upperBound = parseInt('1');
                  if (upperBound >= 0) {
                    slicesOfContent = slicesOfContent.slice(0, upperBound);
                  }

                  // highlight title and content

                  function highlightKeyword(text, slice) {
                    var result = '';
                    var prevEnd = slice.start;
                    slice.hits.forEach(function (hit) {
                      result += text.substring(prevEnd, hit.position);
                      var end = hit.position + hit.length;
                      result += '<b class="search-keyword">' + text.substring(hit.position, end) + '</b>';
                      prevEnd = end;
                    });
                    result += text.substring(prevEnd, slice.end);
                    return result;
                  }

                  var resultItem = '';

                  if (slicesOfTitle.length != 0) {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + highlightKeyword(title, slicesOfTitle[0]) + "</a>";
                  } else {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + title + "</a>";
                  }

                  slicesOfContent.forEach(function (slice) {
                    resultItem += "<a href='" + articleUrl + "'>" +
                      "<p class=\"search-result\">" + highlightKeyword(content, slice) +
                      "...</p>" + "</a>";
                  });

                  resultItem += "</li>";
                  resultItems.push({
                    item: resultItem,
                    searchTextCount: searchTextCount,
                    hitCount: hitCount,
                    id: resultItems.length
                  });
                }
              })
            };
            if (keywords.length === 1 && keywords[0] === "") {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-search fa-5x"></i></div>'
            } else if (resultItems.length === 0) {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-frown-o fa-5x"></i></div>'
            } else {
              resultItems.sort(function (resultLeft, resultRight) {
                if (resultLeft.searchTextCount !== resultRight.searchTextCount) {
                  return resultRight.searchTextCount - resultLeft.searchTextCount;
                } else if (resultLeft.hitCount !== resultRight.hitCount) {
                  return resultRight.hitCount - resultLeft.hitCount;
                } else {
                  return resultRight.id - resultLeft.id;
                }
              });
              var searchResultList = '<ul class=\"search-result-list\">';
              resultItems.forEach(function (result) {
                searchResultList += result.item;
              })
              searchResultList += "</ul>";
              resultContent.innerHTML = searchResultList;
            }
          }

          if ('auto' === 'auto') {
            input.addEventListener('input', inputEventFunction);
          } else {
            $('.search-icon').click(inputEventFunction);
            input.addEventListener('keypress', function (event) {
              if (event.keyCode === 13) {
                inputEventFunction();
              }
            });
          }

          // remove loading animation
          $(".local-search-pop-overlay").remove();
          $('body').css('overflow', '');

          proceedsearch();
        }
      });
    }

    // handle and trigger popup window;
    $('.popup-trigger').click(function(e) {
      e.stopPropagation();
      if (isfetched === false) {
        searchFunc(path, 'local-search-input', 'local-search-result');
      } else {
        proceedsearch();
      };
    });

    $('.popup-btn-close').click(onPopupClose);
    $('.popup').click(function(e){
      e.stopPropagation();
    });
    $(document).on('keyup', function (event) {
      var shouldDismissSearchPopup = event.which === 27 &&
        $('.search-popup').is(':visible');
      if (shouldDismissSearchPopup) {
        onPopupClose();
      }
    });
  </script>





  

  

  
  

  
  

  


  

  

  
  <script>
    (function(){
      var bp = document.createElement('script');
      var curProtocol = window.location.protocol.split(':')[0];
      bp.src = (curProtocol === 'https') ? 'https://zz.bdstatic.com/linksubmit/push.js' : 'http://push.zhanzhang.baidu.com/push.js';
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(bp, s);
    })();
  </script>


  

  

  

  

  

  
<script>
  $('.highlight').not('.gist .highlight').each(function(i, e) {
    var $wrap = $('<div>').addClass('highlight-wrap');
    $(e).after($wrap);
    $wrap.append($('<button>').addClass('copy-btn').append('复制').on('click', function(e) {
      var code = $(this).parent().find('.code').find('.line').map(function(i, e) {
        return $(e).text();
      }).toArray().join('\n');
      var ta = document.createElement('textarea');
      var yPosition = window.pageYOffset || document.documentElement.scrollTop;
      ta.style.top = yPosition + 'px'; // Prevent page scroll
      ta.style.position = 'absolute';
      ta.style.opacity = '0';
      ta.readOnly = true;
      ta.value = code;
      document.body.appendChild(ta);
      ta.select();
      ta.setSelectionRange(0, code.length);
      ta.readOnly = false;
      var result = document.execCommand('copy');
      
        if (result) $(this).text('复制成功');
        else $(this).text('复制失败');
      
      ta.blur(); // For iOS
      $(this).blur();
    })).on('mouseleave', function(e) {
      var $b = $(this).find('.copy-btn');
      setTimeout(function() {
        $b.text('复制');
      }, 300);
    }).append(e);
  })
</script>


  

  

</body>
</html>
